<?php
namespace app\admin\controller;
use app\common\model\Article as ArticleModel;
use app\common\model\Material as MaterialModel;
use app\common\model\Card as CardModel;
use app\common\model\ChatMsgPrivate as ChatMsgPrivateModel;

class Article extends Base
{

    public $articleModel;
    public function __construct () {
        parent::__construct();
        $this->articleModel = new ArticleModel;
        $this->materialModel = new MaterialModel;
        $this->cardModel = new CardModel;
    }

    /* 
     列表
     */
    public function list()
    {
        $where = array();
        $page = max(input('page'),1);
        $size = 20;
        $articleList = $this->articleModel->getPage($where,$page,$size,'ord desc,id desc');
        $total = $this->articleModel->getCount($where);
        if(!$articleList) {
            return returnJson(1,'');
        }
        foreach($articleList as $k=>$v) {
            $articleList[$k]['picture_src'] = tomedia($v['picture']);
        }
        return returnJson(0,'',[
            'list' => $articleList,
            'size' => $size,
            'total' => $total
        ]);
    }

    /* 新增文章/修改文章 */
    public function edit () {
        if(request()->isPost()) {
            $input = input();
            if(!isset($input['title']) || !isset($input['picture'])) {
                return returnJson(1,'请填写完整');
            }
            $data = array(
                'title' => $input['title'],
                'desc' => $input['desc'],
                'picture' => $input['picture'],
                'ord' => $input['ord'],
                'desc' => $input['desc'],
                'content' => $input['content'],
            );
            if($input['id']) {
                $data['id'] = $input['id'];
            }
            if($data['id']) {
                $res = $this->articleModel->isUpdate(true)->save($data);
            }else{
                $res = $this->articleModel->isUpdate(false)->save($data);
            }
            if($res) {
                return returnJson(0,'操作成功');
            }
            return returnJson(1,'网络异常，请重试');
        }
    }

    /* 删除文章 */
    public function delete () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        $article = $this->articleModel->getOne(['id'=>$input['id']]);
        if(!$article) {
            return returnJson(1,'文章不存在');
        }
        $res = $this->articleModel->where(['id'=>$input['id']])->delete();
        if($res) {
            model('qiniu')->remove($article['picture']);
            return returnJson(0,'已删除');
        }else{
            return returnJson(1,'删除失败，请重试');
        }
    }

    /* 素材列表 */
    public function materialList () {
        $where = array();
        $page = max(input('page'),1);
        $size = 20;
        $materialList = $this->materialModel->getPage($where,$page,$size,'ord desc,id desc');
        $total = $this->materialModel->getCount($where);
        if(!$materialList) {
            return returnJson(1,'');
        }
        foreach($materialList as $k=>$v) {
            $pics = json_decode($v['picture_list'],true);
            foreach($pics as $kk=>$vv) {
                $pics[$kk]['url'] = tomedia($vv['path']);
            }
            $materialList[$k]['picture_list'] = $pics;
        }
        return returnJson(0,'',[
            'list' => $materialList,
            'size' => $size,
            'total' => $total
        ]);
    }

    /* 编辑新增素材 */
    public function materialEdit () {
        if(request()->isPost()) {
            if(request()->isPost()) {
                $input = input();
                $pics = $input['picture_list'];
                if(!isset($input['content']) || !isset($input['picture_list'])) {
                    return returnJson(1,'请填写完整');
                }
                $data = array(
                    'picture_list' => $input['picture_list'],
                    'ord' => $input['ord']?$input['ord']:0,
                    'content' => $input['content'],
                    'name' => $input['name'],
                    'goods_id' => $input['goods_id']
                );
                if($input['id']) {
                    $data['id'] = $input['id'];
                }
                if($data['id']) {
                    $res = $this->materialModel->isUpdate(true)->save($data);
                }else{
                    $res = $this->materialModel->isUpdate(false)->save($data);
                }
                if($res) {
                    return returnJson(0,'操作成功');
                }
                return returnJson(1,'网络异常，请重试');
            }
        }
    }

    /* 删除素材 */
    public function materialDelete () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        $material = $this->materialModel->getOne(['id'=>$input['id']]);
        if(!$material) {
            return returnJson(1,'素材不存在');
        }
        $res = $this->materialModel->where(['id'=>$input['id']])->delete();
        if($res) {
            //TODO 删除远程存储的图片
            $pics = json_decode($material['picture_list'],true);
            foreach($pics as $v) {
                model('qiniu')->remove($v['path']);
            }
            return returnJson(0,'已删除');
        }else{
            return returnJson(1,'删除失败，请重试');
        }
    }


    /* 卡片列表 */
    public function cardList () {
        $where = array();
        $page = max(input('page'),1);
        $size = 20;
        $cardList = $this->cardModel->getPage($where,$page,$size,'ord desc,id desc');
        $total = $this->cardModel->getCount($where);
        if(!$cardList) {
            return returnJson(1,'');
        }
        foreach($cardList as $k=>$v) {
            $cardList[$k]['picture_src'] = tomedia($v['picture']);
            $cardList[$k]['content'] = json_decode($v['content'],true);
            $cardList[$k]['only_read'] = $v['only_read']==1?true:false;
        }
        return returnJson(0,'',[
            'list' => $cardList,
            'size' => $size,
            'total' => $total
        ]);
    }

    /* 编辑新增素材 */
    public function cardEdit () {
        if(request()->isPost()) {
            if(request()->isPost()) {
                $input = input();
                if(!isset($input['content'])) {
                    return returnJson(1,'请填写完整');
                }
                $data = array(
                    'ord' => $input['ord']?$input['ord']:0,
                    'content' => $input['content'],
                    'title' => $input['title'],
                    'picture' => $input['picture'],
                    'bg_picture' => $input['bg_picture'],
                    'only_read' => $input['only_read']==true?1:0
                );
                if($input['id']) {
                    $data['id'] = $input['id'];
                }
                if($data['id']) {
                    $res = $this->cardModel->isUpdate(true)->save($data);
                }else{
                    $res = $this->cardModel->isUpdate(false)->save($data);
                }
                if($res) {
                    return returnJson(0,'操作成功');
                }
                return returnJson(1,'网络异常，请重试');
            }
        }
    }

    /* 删除素材 */
    public function cardDelete () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        $card = $this->cardModel->getOne(['id'=>$input['id']]);
        if(!$card) {
            return returnJson(1,'素材不存在');
        }
        $res = $this->cardModel->where(['id'=>$input['id']])->delete();
        if($res) {
            //TODO 删除远程存储的图片
            $pics = json_decode($card['picture_list'],true);
            return returnJson(0,'已删除');
        }else{
            return returnJson(1,'删除失败，请重试');
        }
    }

    /* 添加密训信息 */
    public function privateEdit () {
        $input = input();
        if(!$input['type'] || !$input['content']) {
            return returnJson(1,'请填写完整');
        }
        $data = [
            'type' => $input['type'],
            'content' => $input['content'],
            'ord' => $input['ord']?$input['ord']:0
        ];
        if(!$input['id']) {
            $res = ChatMsgPrivateModel::create($data);
        }else{
            $res = ChatMsgPrivateModel::where('id',$input['id'])->update($data);
        }
        return returnJson(0,'操作成功');
    }

    /* 密训消息列表 */
    public function privateList () {
        $where = array();
        $page = max(input('page'),1);
        $size = 20;
        $list = ChatMsgPrivateModel::getPage($where,$page,$size,'ord asc,id asc');
        $total = ChatMsgPrivateModel::getCount($where);
        if(!$list) {
            return returnJson(0,'',[]);
        }
        $typeArr = [
            'text' => '文字',
            'img' => '图片',
            'voice' => '音频',
            'video' => '视频',
        ];
        foreach($list as $k=>$v) {
            if(in_array($v['type'],[
                'img','voice','video'
            ])) {
                $list[$k]['src'] = tomedia($v['content']);
            }
            $list[$k]['type_text'] = $typeArr[$v['type']];
            if($v['type']=='text') {
                //$list[$k]['content'] = nl2br($v['content']);
            }
        }
        return returnJson(0,'',[
            'list' => $list,
            'size' => $size,
            'total' => $total
        ]);
    }
    /* 删除密训消息 */
    public function privateDelete () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        ChatMsgPrivateModel::where('id',$input['id'])->delete();
        return returnJson(0,'删除成功');
    }

   
}

